home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 46 / Amiga Format CD46 (1999-10-20)(Future Publishing)(GB)[!][issue 1999-12].iso / -in_the_mag- / reader_requests / scilab / demos / control / pid.dem.bak < prev    next >
Text File  |  1999-09-16  |  3KB  |  122 lines

  1. exec(/store3/sci/scilab-2.2/demos/control/scheme.dem');
  2. s=poly(0,'s');z=poly(0,'z');
  3. x_message('Example of PID Design ')
  4. n=x_choose(['Continuous time';'Discrete time'],'Select time domain');
  5. select n
  6.  case 0
  7.   warning('Demo stops!');return;
  8.  case 1
  9.   dom='c';
  10.   s=poly(0,'s');
  11.   str='[(s-1)/(s^2+5*s+1)]';
  12.   rep=x_dialog('Nominal plant?',str)
  13.   if rep==[] then return,end
  14.   Plant=evstr(rep); 
  15.   Plant=syslin('c',Plant);
  16.  case 2
  17.   dom='d'
  18.   z=poly(0,'z');
  19.   str='(z+1)/(z^2-5*z+2)'
  20.   rep=x_dialog('Nominal plant?',str)
  21.   if rep==[] then return,end
  22.   Plant=evstr(rep)
  23.   Plant=syslin('d',Plant);
  24. end   
  25.  //Nominal Plant
  26. P22=tf2ss(Plant);    //...in state-space form
  27. [ny,nu,nx]=size(P22);
  28.    defv=['-1.2','1','0.1'];
  29.    if dom='d' then defv=['-10','1','0.1'];end
  30. while %t
  31.    if dom='c' then
  32.    title='Enter your PID controller K(s)=Kp*(1+T0/s+T1*s)';
  33.    end
  34.    if dom='d' then
  35.    title='Enter your PID controller K(z)=Kp*(1+T0/z+T1*z)';
  36.    end
  37.    defv=x_mdialog(title,['Kp=';'T0=';'T1='],defv);
  38.    if defv=[] then warning('Demo stops!');return;end
  39.    Kp=evstr(defv(1));T0=evstr(defv(2));T1=evstr(defv(3));
  40.    if dom='c' then
  41.    Kpid=tf2ss(Kp*(1+T0/s+T1*s));
  42.    end
  43.    if dom='d' then
  44.    Kpid=tf2ss(Kp*(1+T0/z+T1*z));
  45.    end
  46.    W=[1, -P22;
  47.       Kpid,1];Winv=inv(W);
  48.  
  49.    disp(spec(Winv(2)),'closed loop eigenvalues');//Check internal stability
  50.    if maxi(real(spec(Winv(2)))) > 0 then 
  51.      x_message('You loose: closed-loop is UNSTABLE!!!');
  52.                                     else
  53.      x_message('Congratulations: closed-loop is STABLE !!!');
  54.      break;
  55.    end
  56. end
  57.  
  58. [Spid,Rpid,Tpid]=sensi(P22,Kpid);  //Sensitivity functions
  59. Tpid(5)=clean(Tpid(5));
  60.  
  61. disp(clean(ss2tf(Spid)),'Sensitivity function');
  62. disp(clean(ss2tf(Tpid)),'Complementary sensitivity function');
  63.  
  64. resp=['Frequency response';'Time response'];
  65. while %t do
  66. n=x_choose(resp,'Select response(s)');
  67. if degree(Tpid(5))>0 then
  68.     error('Improper transfer function!')
  69. end
  70. Tpid(5)=coeff(Tpid(5));
  71. select n
  72.  case 0
  73.   break
  74.  case 1
  75.   xbasc(1);xset("window",1);xselect();bode(Tpid);
  76.  case 2
  77.   if Plant(4)='c' then
  78.    defv=['0.1','50'];
  79.    title='Enter Sampling period and Tmax';
  80.    rep=x_mdialog(title,['Sampling period?';'Tmax?'],defv)
  81.    if rep==[] then break,end
  82.    dttmax=evstr(rep);
  83.    dt=evstr(dttmax(1));tmax=evstr(dttmax(2));
  84.    t=0:dt/5:tmax;
  85.    n1=x_choose(['Step response?';'Impulse response?'],'Simulation:');
  86.    if n1==0 then
  87.       warning('Demo stops!');return;
  88.    end
  89.    if n1==1 then 
  90.       xbasc(1);xset("window",1);xselect();
  91.       plot2d([t',t'],[(csim('step',t,Tpid))',ones(t')])
  92.    end
  93.    if n1==2 then
  94.      xbasc(1);xset("window",1);xselect();
  95.      plot2d([t',t'],[(csim('impul',t,Tpid))',0*t'])
  96.    end
  97.   end
  98.   if Plant(4)='d' then
  99.    defv=['100'];
  100.    title='Tmax?'
  101.    rep=x_mdialog(title,['Tmax='],defv)
  102.    if rep==[] then break,end
  103.    tmax=evstr(rep);
  104.    while %t do
  105.    n=x_choose(['Step response?';'Impulse response?'],'Simulation:');
  106.    select n
  107.    case 0 then
  108.      break
  109.    case 1 then
  110.       u=ones(1,Tmax);u(1)=0;
  111.       xbasc(1);xset("window",1);xselect();
  112.       plot2d([(1:tmax)',(1:tmax)'],[(dsimul(Tpid,u))',(ones(1:tmax)')])
  113.    case 2 then
  114.       u=zeros(1,Tmax);u(1)=1;
  115.       xbasc(1);xset("window",1);xselect();
  116.       plot2d((1:Tmax)',(dsimul(Tpid,u))')
  117.    end
  118.    end
  119.   end
  120. end
  121. end
  122.